﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class View_CFG_CFG002 : ViewMatrix
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Title = "餘額與新撥貸驗證";
        criteriaList = new string[] { "date1", "dates","dtList","account" };
        serviceName = "MISAPP.Classify.Service.Service_CFG002";
        if (!IsPostBack)
        { 
            MISAPP.Classify.Service.Service_CFG002 c002 = GetServiceObject() 
                as MISAPP.Classify.Service.Service_CFG002;
            System.Data.DataTable dt = c002.GetEmptyBALTable();
            GV1.DataSource = dt;
            GV1.DataBind();
            GV2.DataSource = dt;
            GV2.DataBind();
        }
    }

    protected void btn_Execute_Click(object sender, EventArgs e)
    {
        if (!Verify())
        {
            return;
        }
        Execute();   
    }
    protected void Execute()
    {
        MISAPP.Classify.Service.Service_CFG002 c002 = GetServiceObject()
               as MISAPP.Classify.Service.Service_CFG002;
        System.Data.DataTable dt1 = c002.GetEmptyBALTable();
        System.Data.DataTable dt2 = c002.GetEmptyBALTable();
        List<System.Data.DataTable> dtList = new List<System.Data.DataTable>();
        FillDadaTableFromGridView(dt1, GV1);
        FillDadaTableFromGridView(dt2, GV2);
        dtList.Add(dt1);
        dtList.Add(dt2);
        string date1 = datePicker1.GetStringDate();
        List<string> dates = datePickerFromTo1.GetBetweenDate();
        Session["date1"] = date1;
        Session["dates"] = dates;
        Session["dtList"] = dtList;
        //勾選了是要排除
        Session["account"] = chkAccount.Checked ? false : true;
        Server.Transfer("CFG002_1.aspx");
    }

    protected bool Verify()
    {
        if (!datePicker1.Validate())
        {
            showMessage("月底日輸入錯誤！");
            return false;
        }
        if (!datePickerFromTo1.Validate())
        {
            showMessage("撥貸起迄日輸入錯誤！");
            return false;
        }
        return true;
    }

    protected void showMessage(string p_msg)
    {
        string alertScript = string.Format("alert('{0}')",p_msg);
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertScript", alertScript, true);
    }

    protected void FillDadaTableFromGridView(System.Data.DataTable p_dt,
        GridView p_gv)
    {
        int i = 0;
        foreach (GridViewRow row in GV1.Rows)
        {
            TextBox txt = row.FindControl("txtMISBAL") as TextBox;
            p_dt.Rows[i]["MIS餘額"] = txt.Text.Length == 0 ? 0:
                Convert.ToInt64(txt.Text);
            i++;
        }
    }
}
